package com.itheima.shop.service;

import com.itheima.shop.dao.CategoryDao;
import com.itheima.shop.entity.Category;
import com.itheima.shop.entity.PageBean;

import java.sql.SQLException;
import java.util.List;

public class CategoryService {
    private CategoryDao dao = new CategoryDao();
    /**
     * 查询所有类别,分页查询
     */
    public PageBean<Category> findAllCategory(int currPageNO, int perPageNO) throws SQLException {
        PageBean<Category> pageBean = new PageBean<>();

        pageBean.setCurrPageNO(currPageNO);
        pageBean.setPerPageNO(perPageNO);

        int allRecordNO = dao.getAllRecordNO();
        pageBean.setAllRecordNO(allRecordNO);

        int allPageNO = allRecordNO % perPageNO == 0 ? allRecordNO / perPageNO : allRecordNO / perPageNO + 1;
        pageBean.setAllPageNO(allPageNO);

        int start = (currPageNO - 1) * perPageNO;
        int size = perPageNO;
        List<Category> category = dao.findAllCategory(start, size);
        pageBean.setList(category);

        return pageBean;
    }
    /**
     * 修改类别
     */
    public boolean updateCategory(int cid,String cname) throws SQLException {
        return dao.updateCategory(cid, cname) > 0;
    }
    /**
     * 删除类别
     */
    public boolean deleteCategory(int cid) throws SQLException {
        return dao.deleteCategory(cid) > 0;
    }
    /**
     * 增加类别
     */
    public boolean addCategory(String cname) throws SQLException {
        return dao.addCategory(cname) > 0;
    }
    /**
     * 查询所有类别,用于填充form表单
     */
    public List<Category> findAllCategoryAjax() throws SQLException {
        return dao.findAllCategoryAjax();
    }
}
